﻿package  {
	
	import flash.display.MovieClip;
	import com.adobe.serialization.json.JSON;
	import core.net.socket.SocketClient;
	import flash.events.Event;
	import core.log.Logger;
	import flash.text.TextField;
	import core.scene.SceneLogin;
	import base.BaseMc;
	import core.scene.SceneBg;
	import core.display.BookCase;
	import core.dialog.DialogBluetoothDiscovery;
	import flash.display.StageDisplayState;
	import core.display.AlphaBg;
	import flash.display.Shape;
	import flash.display.Loader;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.media.Sound;
	import core.events.BgSoundEvent;
	import flash.media.SoundChannel;
	import flash.display.DisplayObject;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import core.net.socket.cmd.TestLoginCmd;
	import core.events.CmdEvent;
	import flash.text.TextFormat;
	
	
	
	[SWF(backgroundColor=0x000000)]//0x54dbff
	public class Main extends BaseMc {
		
		
		private var socketClient:SocketClient;
		private var logger:Logger;
		private var bgSound:Sound;
		private var bgSoundChannel:SoundChannel;
		
		private var testLogin:Timer;
		private var noLoginLabel:TextField;
		private var testLoginCount:int = 0;
		
		private function getStageMask():DisplayObject {
			var mask:MovieClip = new MovieClip();
			mask.graphics.beginFill(0x000000,1);
			mask.graphics.drawRect(0,0,this.stage.stageWidth,stage.stageHeight);
			mask.graphics.endFill();
			mask.width = this.stage.stageWidth;
			mask.height = stage.stageHeight;
			mask.x = 0;
			mask.y = 0;
			return mask;
		}
		
		
		protected override function onInit():void {
			//设置舞台的遮罩
			var m:DisplayObject = this.getStageMask();
			this.addChild(m);
			this.mask = m;
			
			//设置全屏
			//stage.displayState = StageDisplayState.FULL_SCREEN;
			var bg:SceneBg = new SceneBg();
			this.addChild(bg);
			//this.addChild(new DialogBluetoothDiscovery());
			
			this.initNoLoginLabel();
			
			//初始化背景音乐
			this.initBgSound();
			//初始化日志
			this.initLogger();
			//初始化Socket连接
			this.initSocketClient();
			
			this.addEventListener(BgSoundEvent.START,onStartBgSound);
			this.addEventListener(BgSoundEvent.STOP,onStopBgSound);
			
			testLogin = new Timer(5000,1);
			testLogin.addEventListener(TimerEvent.TIMER,onTestLoginTimer);
			testLogin.start();
			
		}
		
		private function initNoLoginLabel():void {
			this.noLoginLabel = new TextField();
			var textFormat:TextFormat = new TextFormat();
			textFormat.size = 20;
			textFormat.bold = true;
			textFormat.color = 0xff0000;
			this.noLoginLabel.defaultTextFormat = textFormat;
			this.noLoginLabel.text = "未登录";
			this.noLoginLabel.x = 20;
			this.noLoginLabel.y = 20;
		}
		
		private function initLogger():void {
			this.logger = Logger.getInstance();
		}
		
		private function initSocketClient():void{
			this.socketClient = SocketClient.getInstance();
			this.socketClient.connectTo("127.0.0.1",9901);
			this.socketClient.addEventListener(Event.CONNECT,onConnected);
		}
		
		
		private function initBgSound():void {
			this.bgSound = new BgSound();
			//this.bgSoundChannel = this.bgSound.play();
			//this.bgSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
		}
		
		private function onConnected(e:Event):void{
			//trace("连接服务端成功");
			this.logger.debug("连接服务端成功");
			
			//启动登录界面
			var sceneLogin:SceneLogin = new SceneLogin();
			this.addChild(sceneLogin);
		}
		
		
		private function onStartBgSound(e:BgSoundEvent):void {
			if(this.bgSoundChannel == null)
			{
				this.bgSoundChannel = bgSound.play();
				this.bgSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
			}
		}
		
		private function onStopBgSound(e:BgSoundEvent):void{
			this.bgSoundChannel.stop();
			this.bgSoundChannel = null;
		}
		
		private function onBgSoundComplete(e:Event):void {
			this.bgSoundChannel = bgSound.play();
			this.bgSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBgSoundComplete);
		}
		
		private function onTestLoginTimer(e:TimerEvent):void {
			this.sendCmd(TestLoginCmd.make());
		}
		
		protected override function onCommandResponse(e:CmdEvent):void {
			super.onCommandResponse(e);
			var data:Object = e.data;
			if(data.cmd == TestLoginCmd.COMMAND_ID){
				var isLogin = data.data["is_login"];
				if(isLogin == 0){
					trace("未登录");
					this.stage.addChild(noLoginLabel);
					if(testLoginCount < 5)
					{
						testLoginCount = testLoginCount + 1;
						testLogin.start();
					}
					
				}else if(isLogin == 1){
					if(this.stage.contains(noLoginLabel)){
						this.stage.removeChild(noLoginLabel);
					}
				}
			}
		}
	}
	
}
